
Skt.RoomList = L.Class.extend({
	
	includes : L.Mixin.Events,
	
	events: ['update','roomModeratorUpdate','roomObjectAction'],
	
	initialize : function(type, sketchpad, group) {
		if ( type == Skt.SlideType.GROUP && group == null )
			console.log("[WARN] Creado roomlist grupal con grupo nulo entregado en constructor");
		
		this._type = type;
		this._sketchpad = sketchpad;
		this._group = group;
		this._rooms = [];
		this._couple(this._sketchpad.getOptions().coupling);
	},

	addRoom: function(roomId, ownerId, index) {
		var self = this;
		if ( !self.containsRoom(roomId) ) {
			var room = new Skt.Room(
				roomId,
				self._type,
				ownerId,
				self
			);
						
			self._rooms.push(room);
			
			room.on('roomModeratorUpdate', self._roomModeratorUpdate, self);
			room.on('objectAction', self._roomObjectAction, self);
	
			self.fire('update', {
				action: 'addRoom',
				room: room,
				index: index,
				roomList: self
			});
		} else
			console.log("[ERROR] RoomList.addRoom self.containsRoom(roomId) == true! ");
	},
	
	removeRoom: function(roomId) {
		var self = this;
		if ( self.containsRoom(roomId) ) {
			var index = self._findIndex(roomId);
			var room = self._rooms[index];
			self._rooms.splice(index, 1);
			
			room.off('roomModeratorUpdate');
			room.off('objectAction');
			
			self.fire('update', {
				action: 'removeRoom',
				room: room,
				index: index,
				roomList: self
			});
		}
	},

	containsRoom: function(roomId) {
		var self = this;
		return self._findIndex(Number(roomId)) >= 0;
	},

	findRoomById: function(roomId) {
		var self = this;
		var index = self._findIndex(Number(roomId));
		if ( index >= 0 )
			return self._rooms[index];
		else
			return null;
	},

	_findIndex: function(roomId) {
		var self = this;
		for ( var i = 0; i < self._rooms.length; i++) {
			if (self._rooms[i].getId() == Number(roomId))
				return i;
		}
		return -1;
	},

	eachRoom : function(fn, context) {
		var self = this;
		var rooms = self._rooms;
		for ( var i = 0, n = rooms.length; i < n; i++)
			fn.call(context, rooms[i]);
	},
	
	getRooms: function() { return this._rooms; },
	getGroup: function() { return this._group; },
	getType: function() { return this._type; },
	getSketchpad: function() { return this._sketchpad; },
	
	_roomModeratorUpdate: function(params) { 
		var self = Skt.RoomList.findSelf(this);
		self.fire('roomModeratorUpdate', params);
	},
	_roomObjectAction: function(params) {
		var self = Skt.RoomList.findSelf(this);
		self.fire('roomObjectAction', params); 
	},
	
	_couple: function(coupling) {
		if ( coupling.enabled ) {
			var adapter = coupling.activityAdapter;
			if ( this._type == Skt.SlideType.PERSONAL )
				adapter = coupling.personalAdapter;
			
			var id = "roomlist-" + this._type;
			if ( this._type == Skt.SlideType.GROUP )
				id = id + this._group.getId();
			
			adapter.coupleObject(
				id,
				this,
				{
					messageType: "EVENT",
					explicitMapping: ["addRoom", "removeRoom"]
				}
			);
		}
	}

});

Skt.RoomList.findSelf = function(roomlist) {
	return Skt.sketchpad.findRoomListByType(roomlist._type, 
			roomlist._group != null ? roomlist._group._id : null);
};

